import CryptoJs from "crypto-js";

const key = "qSh3XONHYcIaaSKa";
const iv = "a4Ldki0jkX3iRndw";
/**
 * CryptoJs 加密方法
 * @param String [str] 加密字符串
 * @return String
 */
export const Encrypt = (str: string) => {
  const keys = CryptoJs.enc.Utf8.parse(key);
  const ivs = CryptoJs.enc.Utf8.parse(iv);
  const strs = CryptoJs.enc.Utf8.parse(str);
  const encrypted = CryptoJs.AES.encrypt(strs, keys, {
    mode: CryptoJs.mode.ECB,
    padding: CryptoJs.pad.Pkcs7,
    iv: ivs,
  });
  return encrypted.ciphertext.toString(CryptoJs.enc.Base64);
};

/**
 * CryptoJs 解密方法
 * @param String [str] 解密字符串
 * @return String
 */
export const Decrypt = (str: string | CryptoJs.lib.CipherParams) => {
  const keys = CryptoJs.enc.Utf8.parse(key);
  const decrypt = CryptoJs.AES.decrypt(str, keys, {
    mode: CryptoJs.mode.ECB,
    padding: CryptoJs.pad.Pkcs7,
  });
  return CryptoJs.enc.Utf8.stringify(decrypt).toString();
};
